import Vue from 'vue';
import VueRouter from 'vue-router';

const routerPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
    return routerPush.call(this, location).catch(error => error)
}

const VueRouterReplace = VueRouter.prototype.replace
VueRouter.prototype.replace = function replace(to) {
    return VueRouterReplace.call(this, to).catch(err => err)
}

Vue.use(VueRouter);

// Home组件是主视图 也就是path为空的时候 包裹三个子路由
const Home = () =>
    import ('views/home/home');
const Index = () =>
    import ('views/home/index/index');
const Detail = () =>
    import ('views/home/detail/detail');


// 购物车页面是单独的路由
const Cart = () =>
    import ('views/cart/cart');

// Order组件是主视图 包裹四个子路由
const Order = () =>
    import ('views/order/order');
const OrderConfirm = () =>
    import ('views/order/orderConfirm');
const OrderList = () =>
    import ('views/order/orderList');
const OrderPay = () =>
    import ('views/order/orderPay');
const AliPay = () =>
    import ('views/alipay/aliPay');

const Login = () =>
    import ('../views/login/login.vue');


const routes = [{
        path: '/login',
        name: 'login',
        component: Login,
    },
    {
        path: '/user',
        name: 'User',
        meta: {
            requireAuth: true
        },
        component: () =>
            import ('../views/user')
    },
    {
        path: '/',
        name: 'home',
        component: Home,
        redirect: '/index',
        children: [{
            path: 'index',
            name: 'index',
            component: Index,
        }, {
            path: 'detail/:id',
            name: 'detail',
            component: Detail,
        }]
    },
    {
        path: '/publish',
        name: 'Publish',
        component: () =>
            import ('../views/publish')
    },
    {
        path: '/daity',
        name: 'Daity',
        component: () =>
            import ('../views/daity')
    },
    {
        path: '/cart',
        name: 'cart',
        meta: {
            requireAuth: true
        },
        component: Cart,
    }, {
        path: '/address',
        name: 'Address',
        meta: {
            requireAuth: true
        },
        component: () =>
            import ('../views/address')
    },
    {
        path: '/comment',
        name: 'Comment',
        meta: {
            requireAuth: true
        },
        component: () =>
            import ('../views/comment')
    },
    {
        path: '/order',
        name: 'order',
        meta: {
            requireAuth: true
        },
        component: Order,
        redirect: '/order/list',
        children: [{
                path: 'detail',
                name: 'order-detail',
                meta: {
                    requireAuth: true
                },
                component: () =>
                    import ('../views/order/orderDetail.vue')
            },
            {
                path: 'confirm',
                name: 'order-confirm',
                meta: {
                    requireAuth: true
                },
                component: OrderConfirm,
            }, {
                path: 'list',
                name: 'order-list',
                component: OrderList,
            }, {
                path: 'pay',
                name: 'order-pay',
                component: OrderPay,
            }, {
                path: 'alipay',
                name: 'ali-pay',
                component: AliPay,
            },
        ],
    },
    {
        path: '/detailList',
        name: 'DetailList',
        meta: {
            requireAuth: true
        },
        component: () =>
            import ('../views/home/detail/detailList.vue')
    }
];


const router = new VueRouter({
    routes,
    mode: 'history',
})

export default router;